package com.example.myapplication;
//C:\Users\liuzc\Desktop\MyApplication2\app\src\main\java\com\example\myapplication\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;


import java.util.Random;
import java.util.logging.LogRecord;

public class MainActivity extends AppCompatActivity {

    private Button toggleButton;
    private TextView textView;
    private String[] data = {"文字1", "文字2", "文字3"};
    private int currentIndex = 0;
    private Handler handler = new Handler();
    private Runnable textSwitcher;
    //---------------------------------------------area of function--------------------------
    public String[] addElementToStringArray(String[] oldArray, String newElement) {   //这个是动态添加元素
        // 创建新的String数组
        String[] newArray = new String[oldArray.length + 1];
        // 复制原有的String数组
        System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
        // 添加新的String元素
        newArray[oldArray.length] = newElement;
        return newArray;
    }

    public  String[] returnzero() {        //这个是返回一个长度为0的string数组
        String[] arrNew = new String[0];
        return arrNew;
    }

    //---------------------------------------------end of area of function------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //----------------------area1-------------------------------------------
        MyDatabaseHelper dbHelper=new MyDatabaseHelper(this,"BookStore.dp",null,2);
        toggleButton = findViewById(R.id.toggleButton);
        textView = findViewById(R.id.textView);
        ImageView nextpage=findViewById(R.id.nextpage);
        //-----------------------end of area1-----------------------------
        //---------------------------database--------------------------------
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        data=returnzero();  //清零data
        //查询Book中的所有数据
        Cursor cursor=db.query("Book",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                //遍历Cursor对象，取出数据并打印
                String name=cursor.getString(cursor.getColumnIndex("name"));

                Log.d("MainActivity23","Book is "+name);
//                Toast.makeText(Second.this,name,Toast.LENGTH_SHORT).show();
                data=addElementToStringArray(data,name);   //还要记得赋值，不然没有反应
            }while (cursor.moveToNext());

        }

        //--------------------------end of database-----------------------
        //---------------------------------------------------------------
        textSwitcher = new Runnable() {
            @Override
            public void run() {
                Random random = new Random();
                int min = 0; // 最小值
                int max = data.length-1; // 最大值

                // 生成一个[min, max]范围内的随机整数（包括min和max）
                int randomInt = random.nextInt(max - min + 1) + min;

                currentIndex = randomInt;
                textView.setText(data[currentIndex]);
                // 再次延迟一定时间后执行自己，形成循环
                handler.postDelayed(this, 45); // 延迟500毫秒
            }
        };
        //------------------------------end------------------------------------
        //---------------------------------area2---------------------------------
        toggleButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // 开始切换文字
                handler.postDelayed(textSwitcher, 0);

                // 这里需要返回一个false，因为我们不希望消耗长按事件，
                // 以便用户可以通过移动手指离开按钮来触发UP事件
                return false;
            }
        });
        //----------------------------------area3-----------------------------------
        //---------------------------------area2---------------------------------
        toggleButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                    // 用户释放按钮或触摸被取消时停止切换
                    handler.removeCallbacks(textSwitcher);
                }
                return false; // 返回false以允许其他触摸监听器接收事件
            }
        });
        //----------------------------------area3-----------------------------------
        nextpage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Second.class);
                startActivity(intent);
            }
        });
        //--------------------------------end of area3---------------------------
        //----------------------------------area3-----------------------------------

        //--------------------------------end of area3---------------------------
    }
}